<?php

namespace App\Admin\Controllers;

use App\Area;
use App\City;
use App\Park;
use App\Workshop;
use App\Matching;
use App\Broker;
use App\Http\Controllers\Controller;
use App\WorkshopIndustry;
use App\WorkshopPropertyType;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\MessageBag;

class WorkshopController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('厂房管理')
            ->description('厂房管理')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('厂房管理详情')
            ->description('厂房管理详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('厂房管理编辑')
            ->description('厂房管理编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content, Request $request)
    {

        if (!$request->reflash) {
            return redirect('/admin/workshop/create?reflash=1');
        }
        return $content
            ->header('厂房管理创建')
            ->description('厂房管理创建')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Workshop);

        $grid->id('Id');
        $grid->title('厂房标题');
       // $grid->rent('租金');
        //$grid->price('单位价格');
        $grid->type('出租/出售类型')->display(function ($value) {
            if ($value == 0) {
                return '出租';
            } else if ($value == 1) {
                return '出售';
            } else {
                return '可租可售';
            }
        });
        //$grid->workshop_property_type_id('物业类型')->display(function ($value) {
         //   return WorkshopPropertyType::find($value)->name ?? '';
       // });
        $grid->area('厂房面积(㎡)');
        // $grid->tag('标签');
        // $grid->address('厂房地址');
        //$grid->built_area('建筑面积');
        //$grid->high('首层高度');
        //$grid->floor('楼层数');
        //$grid->elevator('电梯类型');
        //$grid->distribution('配电类型');
        $grid->code('厂房编号');
        //$grid->id_hot('推荐');
        $grid->click_count('点击量');
        $grid->call_count('电话拨打次数');
        //$grid->owner('业主名称');
        //$grid->ownertel('业主电话');
        $grid->park_id('园区')->display(function ($value) {
            return Park::find($value)->title ?? '';
        });
        $grid->is_out('来自')->display(function ($value) {
            if($value==0){
                return "来自本站";
            }else{
                return "来自58同城";
            }
        });
        /* $grid->workshop_industry_id('厂房行业')->display(function ($value){
             return WorkshopIndustry::getSelectOption($value) ?? '' ;
         });*/
        $grid->column('yulan', '预览')->display(function ($vlaue) {
            return "<a href='" . config('admin.app_outurl') . "/detail/{$this->id}' target='_blank'> 查看</a>";
        });
        $grid->owner_release('是否业主发布')->display(function ($value) {
            if ($value == 0) {
                return '否';
            } else if ($value == 1) {
                return '是';
            }
        });

// 设置text、color、和存储值
        $states = [
            '不推荐', '推荐'
        ];
        $grid->is_recommend('是否推荐')->select($states);


        //$grid->desc('厂房描述');
        $grid->updated_at('更新时间');

        //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $type = [
                    ' ' => '全部',
                    0 => '出租',
                    1 => '出售',
                    2 => '可租可售',
                ];
                $filter->disableIdFilter();
                $filter->equal('is_recommend', '是否推荐')->select([
                    ' ' => '全部',
                    0 => '不推荐',
                    1 => '推荐',
                ]);
                $filter->equal('p_province_id', '城市')->select(City::getSelectOptions());
                if (\Request::get('p_province_id')) {
                    $filter->equal('p_city_id', '区域')->select(City::getSelectOptions(\Request::get('p_province_id')));
                }
                if (\Request::get('p_city_id')) {
                    $filter->equal('p_district_id', '下级区域')->select(City::getSelectOptions(\Request::get('p_city_id')));
                }
                $filter->equal('type', '租售类型')->select($type);
                $filter->equal('park_id', '园区')->select(Park::getSelectOptions());
            });

            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '厂房名称');
                $filter->like('code', '厂房编号');
                $is_type = [
                    0 => '来自本站',
                    1 => '来自58同城',
                ];
                $filter->equal('is_out', '来源')->select($is_type);
                $filter->where(function ($query) {
                    $broker = Broker::where('true_mobile','like',$this->input)->first();
                    $brokerId = $broker?$broker->id:"";
                    $query->where('owner',$brokerId);
                }, '经纪人电话');
            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Workshop::findOrFail($id));

        $show->id('Id');
        $show->title('厂房标题');
        $show->rent('租金');
        $show->price('单位价格');

        $show->type('出租/出售类型')->as(function ($value) {
            if ($value == 0) {
                return "出租";
            } else if ($value == 1) {
                return "出售";
            } else {
                return "可租可售";
            }
        });
        $show->workshop_property_type_id('物业类型')->as(function ($value) {
            return WorkshopPropertyType::find($value)->name ?? '';
        });
        $show->area('建筑面积');
        $show->tag('标签');
        $show->address('厂房地址');
        $show->built_area('建筑面积');
        $show->high('首层高度');
        $show->floor('楼层数');
        $show->elevator('电梯类型');
        $show->distribution('配电类型');
        $show->code('厂房编号');
        $show->is_recommend('是否推荐')->as(function ($value) {
            if ($value == 0) {
                return "不推荐";
            } else {
                return "推荐";
            }
        });
        $show->is_out('来源')->as(function ($value) {
            if ($value == 0) {
                return "来自本站";
            } else {
                return "来自58同城";
            }
        });
        $show->owner_release('是否业主发布')->as(function ($value){
            if($value=1){
                return '是';
            }else{
                return '否';
            }
        });
        $show->click_count('点击量');
        $show->call_count('电话拨打次数');
        $show->owndesc('业主描述');
        $show->owner('业主名称')->as(function($value){
            return Broker::find($value)->name;
        });
        //$show->ownertel('业主电话');
        $show->park_id('园区id');
        $show->workshop_industry_id('厂房行业')->as(function ($value) {
            return WorkshopIndustry::getSelectOption($value) ?? '';
        });
        //$show->desc('简介');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Workshop);
        $type = [
            0 => '出租',
            1 => '出售',
            2 => '可租可售',
        ];
        $form->radio('type', '出租/出售类型')->options($type);
        $form->tab('基础信息',function($form)use ($type){
            $form->text('title', '厂房标题')->rules('required', [
                'required' => '请输入厂房标题'
            ])->help('必填项');
            $form->select('p_province_id', '城市')->options(City::getSelectOptions())->load('p_city_id', '/admin/api/area')->rules('required', [
                'required' => '请选择城市'
            ]);
            $form->select('p_city_id', '区域')->options(function ($id) {
                $id = $id ?: old('p_city_id');
                if($id)
                return City::option($id);
            })->load('p_district_id', '/admin/api/area1')->rules('required', [
                'required' => '请选择区域'
            ]);
            $form->select('p_district_id', '下级区域')->options(function ($id) {
                $id = $id ?: old('p_district_id');
                if($id)
                return City::option($id);
            });
            //$form->image('logo', '厂房图片')->removable()->help('不填则取轮播图第一张');
            $form->uploader('slider', '轮播图')->help('必填项');
            $form->checkbox('matching_id', '配套设施')->options(Matching::getSelectOptions())->help('可以多选');
            $form->number('property', '产权年限');
            $form->select('workshop_property_type_id', '物业类型')->options(WorkshopPropertyType::getSelectOptions())->rules('required', [
                'required' => '请选择物业类型'
            ]);
            $form->number('area', '厂房面积（平方米）')->rules('required', [
                'required' => '请输入厂房面积'
            ]);
            $form->number('built_area', '建筑面积(㎡)');
            /*$form->select('area_id', '面积范围')->options(Area::getSelectOptions())->rules('required', [
                'required' => '请选择面积范围'
            ]);*/
            $form->tags('tag', '标签')->help('添加输入后点击空白处');
            $form->text('address', '厂房地址');
            //$form->map('latitude', 'longitude', '地址与周边设施');
            $form->currency('high', '首层高度')->symbol("m");
            $form->number('floor', '楼层数');
            $form->currency('elevator', '电梯类型')->symbol("T");
            $form->currency('distribution', '配电类型')->symbol("KV");
            $form->hidden('code', '厂房编号')->default(Carbon::now()->timestamp);

            //$form->mobile('ownertel', '业主电话')->options(['mask' => '999 9999 9999']);


            $form->multipleSelect('workshop_industry_id', '厂房适用行业')->options(WorkshopIndustry::getSelectOptions())->rules('required', [
                'required' => '请选择厂房适用行业'
            ]);
            //  $form->textarea('desc', '厂房描述');
            $states = [
                'on' => ['value' => 1, 'text' => '推荐', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '不推荐', 'color' => 'danger'],
            ];
            $is_type = [
                0 => '来自本站',
                1 => '来自58同城',
            ];
            $form->radio('is_out', '来源')->options($is_type);
            $form->switch('is_recommend', '是否推荐')->states($states);


        })->tab('业主信息',function(Form $form){
            $is_own = [
                0 => '否',
                1 => '是',
            ];
            $form->radio('owner_release', '是否业主发布')->options($is_own);
            $form->ckeditor('owndesc', '业主描述');
            $form->select('owner', '经纪人名称')->options(Broker::getSelectOptions())->rules('required', [
                'required' => '请选择经纪人'
            ]);
        })->tab('出租信息',function(Form $form){
            $form->currency('rent', '租金')->symbol("元/月")->help('不填写则面议');
        })->tab('出售信息',function(Form $form){
            $form->currency('price', '每平价格')->symbol("元/㎡")->help('不填写则面议');
            $form->number('payratio', '首付比例');
            $house_type = [
                0 => '现房',
                1 => '预售',
            ];
            $form->radio('house_type', '售出类型')->options($house_type);
        })->tab('园区信息',function(Form $form){
            $form->select('park_id', '园区')->options(Park::getSelectOptions());
        });

        $form->saving(function (Form $form){
            if(sizeof($form->workshop_industry_id)>4){
                $error = new MessageBag([
                    'title'   => '行业类型最多选择3个',
                    'message' => '',
                ]);
                return back()->with(compact('error'))->withInput();
            }
        });

        return $form;
    }
}
